package com.wyp.didishige.fragment.register;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wyp.didishige.fragment.FragmentBase;
import com.wyp.didishige.utils.ToastUtil;
import com.wyp.didishige.R;
import com.wyp.didishige.activity.RegisterActivity;

import java.util.Timer;
import java.util.TimerTask;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.RequestSMSCodeListener;

/**
 * 验证手机验证码是否正确
 */
public class RegisterPhoneVerifyCodeFragment extends FragmentBase {

    public AppCompatEditText code;

    AppCompatButton sendCode;
    View view;

    RegisterActivity registerActivity;

    Timer sendTimer;
    TimerTask sendTask;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initParamter();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.register_verify_phone_fragment, null);
        init();
        return view;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        code = (AppCompatEditText) view.findViewById(R.id.code);
        sendCode = (AppCompatButton) view.findViewById(R.id.sendCode);
    }

    @Override
    protected void initParamter() {
        super.initParamter();
        registerActivity = (RegisterActivity) getActivity();
        sendTimer=new Timer();
        sendTask=new TimerTask() {
            @Override
            public void run() {
                second--;
                Message msg=new Message();
                msg.what=1;
                handler.sendMessage(msg);
            }
        };
    }

    @Override
    protected void initWidgetState() {
        super.initWidgetState();

    }

    @Override
    protected void initWidgetListener() {
        super.initWidgetListener();
        sendCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendPhoneCode();
            }
        });
    }

    /**
     * 发送手机验证码
     */
    public void sendPhoneCode() {
        BmobSMS.requestSMSCode(getActivity(), registerActivity.registerUser.getMobilePhoneNumber(), "师哥帐号注册模板", new RequestSMSCodeListener() {
            @Override
            public void done(Integer integer, BmobException e) {
                if (e == null) {
                    sendCode.setEnabled(false);
                    ToastUtil.showShort(getActivity(), "短信验证码已发送，请注意查收");
                    sendTimer.schedule(sendTask,1000,1000);
                } else {
                    ToastUtil.showShort(getActivity(), String.format("短信发送失败，%s，请稍候再试", e.getLocalizedMessage()));
                }
            }
        });
    }

    int second=60;
    private Handler handler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    if (second<=0){
                        second=60;
                        sendCode.setText("发送");
                        sendTimer.cancel();
                    }else{
                        sendCode.setText(String.format("%s秒后",second));
                    }
                    break;
            }
            return false;
        }
    });
}
